/**
 * @class   wosh::gui::FormSystemMonitor
 * @brief   Graphic interface of wosh::SystemMonitor core-module.
 *
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.1.png "FormSystemMonitor::Tab 1"
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.2.png "FormSystemMonitor::Tab 2"
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.3.png "FormSystemMonitor::Tab 3"
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.4.png "FormSystemMonitor::Tab 4"
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.5.png "FormSystemMonitor::Tab 5"
 * \image html extras/screenshots/WOSH.GUI.FormSystemMonitor.6.png "FormSystemMonitor::Tab 6"
 *
 * @ingroup Gui
 ****************************************************************************
 * @version 0.8.161 $Id: FormSystemMonitor.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_FormSystemMonitor_H__
 #define __WOSH_Gui_FormSystemMonitor_H__

 #include <framework/gui/IWidget.h>
 #include <QAbstractTableModel>
 #include <core/List.h>
 #include <core/Property.h>
 #include <framework/gui/GuiUtilities.h>



namespace Ui {
 class FormSystemMonitor;
};
namespace wosh {
 namespace gui {



class PropertiesListTableModel : public QAbstractTableModel
 {
	Q_OBJECT

	public:
		PropertiesListTableModel(const List* props_list, QObject *parent = 0)
			: QAbstractTableModel(parent) {
			loadProps(props_list);
		 }
		virtual ~PropertiesListTableModel() {
			if ( this->props != NULL )
				delete this->props; this->props = NULL;
		 }

		void loadProps( const List* props_list ) {
			if ( props_list == NULL )
				this->props = NULL;
			else
				this->props = props_list->clone();
			reset();
		 }

	public:
		virtual int rowCount(const QModelIndex &parent = QModelIndex()) const {
			(void)parent;
			if ( this->props == NULL ) return 0;
			return this->props->size();
		 }

		virtual int columnCount(const QModelIndex &parent = QModelIndex()) const {
			(void)parent; return 2;
		 }

		virtual QVariant data(const QModelIndex &index, int role) const {
			if (  !index.isValid() ) return QVariant();
			if ( this->props == NULL ) return QVariant();
			const ISerializable* obj = this->props->itemAt(index.row());
			if ( obj == NULL ) return QVariant();
			const Property* prop = obj->as<Property>();
			if ( prop == NULL ) return QVariant();
			if ( role == Qt::DisplayRole || role == Qt::EditRole ) {
				switch(index.column()) {
					case 0: return QVariant( prop->getKey().c_str() );
					case 1: return GuiUtilities::convertVariant2QVariant(&prop->getValue());
					default: return QVariant();
				 }
			 }
			else if ( role == Qt::BackgroundColorRole ) { }
			else if ( role == Qt::ToolTipRole ) {
				return QString("wosh::Variant [%1]").arg(prop->getValue().getTypeAsString());
			 }
			return QVariant();
		 }

		virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const {
			if (role != Qt::DisplayRole) return QVariant();
			 if (orientation == Qt::Horizontal ) {
				switch(section) {
					case 0: return QVariant("Field");
					case 1: return QVariant("Value");
					default: return QVariant();
				 }
			 }
			return QVariant();
		 }

	private:
		List* props;

}; // class def



class FormSystemMonitor :  public IWidgetGeneric
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::FormSystemMonitor)
	WOSH_CLASS_ABSTRACT(false)


	public:
		FormSystemMonitor(QWidget *parent = 0);
		virtual ~FormSystemMonitor();

	public:
		void busMessage( const Message& message, const Bus* source );

	public slots:
		void refreshAll();

	protected slots:
		void refreshSummary();
		void refreshSysInfo();
		void refreshProfiling();
		void refreshDSM();

		void tableViewSystemErrors_doubleClicked( const QModelIndex& index );

	protected:
		void changeEvent(QEvent *e);

	protected:
		PropertiesListTableModel* sysInfoModel;

	private:
		Ui::FormSystemMonitor *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_FormSystemMonitor_H__
